package com.cn.hotel.dao.Impl;

import com.cn.hotel.dao.IServiceItemsDao;
import com.cn.hotel.model.ServiceItemsModel;
import com.cn.hotel.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ServiceItemsImpl implements IServiceItemsDao {
    @Override
    public List<ServiceItemsModel> findAll(ServiceItemsModel model) {
        StringBuilder sql = new StringBuilder("select * from service_type where 1 = 1 ");
        if (model.getServiceType()!=null&&!"".equals(model.getServiceType())){
            sql.append(" and type_name like'%"+model.getServiceType()+"%'");
        }
        sql.append(" order by id asc limit " + (model.getPageModel().getPageNum() - 1) * model.getPageModel().getPageSize() + "" +
                "," + model.getPageModel().getPageSize() + "");
        ResultSet resultSet = JDBC.getResultSet(sql.toString());
        List<ServiceItemsModel> list = new ArrayList<>();
        try{
            while (resultSet.next()){
                ServiceItemsModel model1 = new ServiceItemsModel();
                model1.setId(resultSet.getInt("id"));
                model1.setServiceType(resultSet.getString("type_name"));
                model1.setShowHref(resultSet.getString("show_href"));
                list.add(model1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 获取总条数
     * @param model
     * @return
     */
    @Override
    public int getNumber(ServiceItemsModel model) {
        StringBuilder sql = new StringBuilder("select count(*) count from service_type where 1 = 1 ");
        if (model.getServiceType()!=null&&!"".equals(model.getServiceType())){
            sql.append(" and type_name like'%"+model.getServiceType()+"%'");
        }
        ResultSet resultSet = JDBC.getResultSet(sql.toString());
        int count = 0;
        try{
            resultSet.next();
            count=resultSet.getInt(1);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    /**
     * 增加
     * @param serviceItemsModel
     * @return
     */
    @Override
    public int add(ServiceItemsModel serviceItemsModel) {
        StringBuilder sql = new StringBuilder();
        if (!"".equals(serviceItemsModel.getServiceType())&&!"".equals(serviceItemsModel.getShowHref())){
            sql.append("insert into service_type(type_name,show_href) values('"+serviceItemsModel.getServiceType()+"'," +
                    "'"+serviceItemsModel.getShowHref()+"')");
        }
        return JDBC.executeUpdate(sql.toString());
    }

    /**
     * 根据id查询
     * @param temp
     * @return
     */
    @Override
    public ServiceItemsModel findById(Integer temp) {
        StringBuilder sql = new StringBuilder();
        if (temp!=0){
            sql.append("select * from service_type where id ="+temp);
        }
        ResultSet resultSet = JDBC.getResultSet(sql.toString());
        ServiceItemsModel model = new ServiceItemsModel();
        try{
            while (resultSet.next()){
                model.setId(resultSet.getInt("id"));
                model.setServiceType(resultSet.getString("type_name"));
                model.setShowHref(resultSet.getString("show_href"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return model;
    }
//保存修改
    @Override
    public int edit(ServiceItemsModel serviceItemsModel) {
        StringBuilder sql = new StringBuilder("update service_type set");
        if (serviceItemsModel.getServiceType()!=null&&!"".equals(serviceItemsModel.getServiceType())){
            sql.append(" type_name = '"+serviceItemsModel.getServiceType()+"',");
        }
        if (serviceItemsModel.getShowHref()!=null&&!"".equals(serviceItemsModel.getShowHref())){
            sql.append(" show_href = '"+serviceItemsModel.getShowHref()+"',");
        }
        sql.append(" remark = '' where id = "+serviceItemsModel.getId()+"");
        return JDBC.executeUpdate(sql.toString());
    }
//删除
    @Override
    public int delete(Integer temp) {
        StringBuilder sql = new StringBuilder();
        sql.append("delete from service_type where id="+temp);
        return JDBC.executeUpdate(sql.toString());
    }

    /**
     * 获取所有的服务项目
     * @return
     */
    @Override
    public List<ServiceItemsModel> findType() {
        StringBuilder sql = new StringBuilder("select type_name from service_type");
        ResultSet resultSet = JDBC.getResultSet(sql.toString());
        List<ServiceItemsModel> list = new ArrayList<>();
        try{
            while (resultSet.next()){
                ServiceItemsModel model = new ServiceItemsModel();
                model.setServiceType(resultSet.getString("type_name"));
                list.add(model);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * ---------------------------------------前端调值👇👇👇👇👇👇👇👇👇----------------------------------------------
     * @return
     */
    @Override
    public List<ServiceItemsModel> getServiceItemsModel() {
        StringBuilder sql = new StringBuilder("select * from service_type");
        ResultSet resultSet = JDBC.getResultSet(sql.toString());
        List<ServiceItemsModel> list = new ArrayList<>();
        try{
            while (resultSet.next()){
                ServiceItemsModel model1 = new ServiceItemsModel();
                model1.setServiceType(resultSet.getString("type_name"));
                model1.setShowHref(resultSet.getString("show_href"));
                list.add(model1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
}
